package com.atguigu.gmall.realtime.common.function;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.realtime.common.constant.Constant;
import com.atguigu.gmall.realtime.common.util.HBaseUtil;
import com.atguigu.gmall.realtime.common.util.RedisUtil;
import io.lettuce.core.api.StatefulRedisConnection;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.async.ResultFuture;
import org.apache.flink.streaming.api.functions.async.RichAsyncFunction;
import org.apache.hadoop.hbase.client.AsyncConnection;

import java.util.Collections;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * ClassName: AsyncDimFunction
 * Description:
 *
 * @Create 2024/4/28 14:19
 */
public abstract class DimAsyncFunction<T> extends RichAsyncFunction<T, T> implements DimFunction<T> {

    private AsyncConnection asyncHbaseConn;

    private StatefulRedisConnection<String, String> asyncRedisConn;

    @Override
    public void open(Configuration parameters) throws Exception {
        asyncHbaseConn = HBaseUtil.getAsyncHBaseConnection();
        asyncRedisConn = RedisUtil.getAsyncRedisConnection();
    }

    @Override
    public void close() throws Exception {
        HBaseUtil.closeAsyncHBaseConn(asyncHbaseConn);
        RedisUtil.closeAsyncRedisConnection(asyncRedisConn);
    }

    @Override
    public void asyncInvoke(T obj, ResultFuture<T> resultFuture) throws Exception {
        //创建异步编排对象，执行线程任务，有返回结果，这个线程任务的结果将作为下一个线程任务的入参
        CompletableFuture.supplyAsync(
                new Supplier<JSONObject>() {
                    @Override
                    public JSONObject get() {
                        //从Redis中获取维度数据
                        JSONObject dimJsonObj = RedisUtil.readDimAsync(asyncRedisConn, getTableName(), getRowKey(obj));

                        return dimJsonObj;
                    }
                }
        ).thenApplyAsync(
                //有入参，有返回值
                new Function<JSONObject, JSONObject>() {
                    @Override
                    public JSONObject apply(JSONObject dimJsonObj) {
                        if (dimJsonObj != null) {
                            //如果获取到了，缓存命中
                            System.out.println("从Redis中获取" + getTableName() + "的" + getRowKey(obj) + "数据");

                        } else {
                            //如果没有找到，发送请求到HBase中查询数据
                            dimJsonObj = HBaseUtil.getRowAsync(asyncHbaseConn, Constant.HBASE_NAMESPACE, getTableName(), getRowKey(obj));
                            if (dimJsonObj != null) {
                                System.out.println("从HBase中获取" + getTableName() + "的" + getRowKey(obj) + "数据");
                                //将查询出来的维度数据缓存到Redis，方便下次查询
                                RedisUtil.writeDimAsync(asyncRedisConn, getTableName(), getRowKey(obj), dimJsonObj);
                            } else {
                                System.out.println("从Hbase中没有找到" + getTableName() + "的" + getRowKey(obj) +"数据");
                            }
                        }

                        return dimJsonObj;
                    }
                }
        ).thenAcceptAsync(
                //有入参，无返回值
                new Consumer<JSONObject>() {
                    @Override
                    public void accept(JSONObject dimJsonObj) {
                        //将维度属性补充到流中的对象上
                        if (dimJsonObj != null) {
                            addDims(obj, dimJsonObj);
                        }

                        //将结果向下游传递
                        resultFuture.complete(Collections.singleton(obj));
                    }
                }
        );
    }

    @Override
    public void timeout(T input, ResultFuture<T> resultFuture) throws Exception {
        throw new RuntimeException("如果异步关联维度超时，可以做如下检查：\n" +
                "1.是否相关组件都启动了：zk、kafka、hdfs、hbase、redis\n" +
                "2.查看Redis中bind的配置是否注掉，或者0.0.0.0\n" +
                "3.查看HBase维度表中是否存在维度数据\n" +
                "4.找昌平彦祖\n");
    }
    
}




